package com.education.springbootdemo.exception;

import com.education.springbootdemo.domain.Resp;
import jakarta.validation.ConstraintViolationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(value = ConstraintViolationException.class)
    public Resp<Exception> constraintViolationException(ConstraintViolationException e) {
        return Resp.error(400, String.format("参数校验异常：%s",e.getMessage()));
    }

//    @ExceptionHandler(value = Exception.class)
//    public Resp<Exception> exception(Exception e) {
//
//        e.printStackTrace();
//        return Resp.error(500, String.format("服务异常：%s",e.getMessage()));
//    }

    //自定义异常
    @ExceptionHandler(value = UserBizException.class)
    public Resp<Exception> userBizException(UserBizException e) {
        return Resp.error(1002, String.format("用户模块异常：%s",e.getMessage()));
    }

    @ExceptionHandler(value = NoAuthExpetion.class)
    public Resp<Exception> noAuthExpetion(NoAuthExpetion e) {
        return Resp.error(401, String.format("登录模块异常：%s",e.getMessage()));
    }
}
